package org.cid54.jshopper.controller.csv;

import java.util.List;
import org.cid54.jshopper.controller.csv.util.GuidChecker;
import org.cid54.jshopper.model.Item;
import org.cid54.jshopper.model.ShopItem;
import org.cid54.jshopper.model.ShoppingList;
import org.cid54.jshopper.model.User;
import org.cid54.jshopper.model.UserGroup;
import org.cid54.jshopper.service.ShopItemService;
import org.cid54.jshopper.service.ShoppingListService;
import org.cid54.jshopper.service.UserAndGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author fgo
 */
@Controller
@RequestMapping("csv/{userGuid}/shoppingLists")
public class CsvShoppingListController {

    @Autowired
    private GuidChecker guidChecker;
    @Autowired
    private ShoppingListService shoppingListService;
    @Autowired
    private ShopItemService shopItemService;
    @Autowired
    private UserAndGroupService userAndGroupService;

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public String doGetAll(@PathVariable("userGuid") String userGuid) {

        if (this.guidChecker.checkGuid(userGuid)) {

            User user = this.userAndGroupService.getUserByMobileGuid(userGuid);

            List<ShoppingList> shoppingLists = this.shoppingListService.getAll(user.getId());

            StringBuilder sb = new StringBuilder();

            for (ShoppingList shoppingList : shoppingLists) {

                sb.append(shoppingList.getId()).append(";");
                sb.append(shoppingList.getName()).append("\n");

            }

            return sb.toString();

        } else {

            return "";

        }

    }

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<String> addShoppingList(@PathVariable("userGuid") String userGuid, @RequestParam("name") String name, @RequestParam("usergroup") String userGroup) {
        if (this.guidChecker.checkGuid(userGuid)) {
            ShoppingList shoppingList = new ShoppingList();

            shoppingList.setName(name);

            shoppingList.setUserGroup(new UserGroup());

            shoppingList.getUserGroup().setName(userGroup);

            this.shoppingListService.create(shoppingList);

            return new ResponseEntity<String>(HttpStatus.OK);
        } else {
            return new ResponseEntity<String>(HttpStatus.UNAUTHORIZED);
        }
    }

    @RequestMapping(value = "{shoppingListId}/create", method = RequestMethod.POST)
    public ResponseEntity<String> addShopItem(@PathVariable("userGuid") String userGuid, @PathVariable("shoppingListId") long shoppingListId, @RequestParam("name") String itemName, @RequestParam("amount") String amount) {
        if (this.guidChecker.checkGuid(userGuid)) {
            User user = this.userAndGroupService.getUserByMobileGuid(userGuid);

            ShopItem item = new ShopItem();

            item.setAmount(Integer.parseInt(amount));
            item.setItem(new Item());
            item.getItem().setName(itemName);

            ShoppingList list = this.shoppingListService.getShoppingListById(shoppingListId);

            item.setShoppingList(list);

            String userName = this.userAndGroupService.getUserNameById(user.getId());

            this.shopItemService.create(item, userName);

            return new ResponseEntity<String>(HttpStatus.OK);
        } else {
            return new ResponseEntity<String>(HttpStatus.UNAUTHORIZED);
        }

    }

    @RequestMapping(value = "{shoppingListId}", method = RequestMethod.GET)
    @ResponseBody
    public String doGetAllShopItems(@PathVariable("userGuid") String userGuid, @PathVariable("shoppingListId") long shoppingListId) {
        if (this.guidChecker.checkGuid(userGuid)) {
            ShoppingList shoppingList = this.shoppingListService.getShoppingListById(shoppingListId);

            StringBuilder sb = new StringBuilder();

            for (ShopItem item : shoppingList.getShopItems()) {
                sb.append(item.getId()).append(";");
                sb.append(item.getItem().getName()).append(";");
                sb.append(item.getAmount()).append(";");
                sb.append(item.isBought()).append("\n");
            }

            return sb.toString();
        } else {

            return "";

        }

    }

    @RequestMapping(value = "{shoppingListId}/delete", method = RequestMethod.POST)
    public ResponseEntity<String> doDeleteShoppingList(@PathVariable("userGuid") String userGuid, @PathVariable("shoppingListId") long shoppingListId) {
        if (this.guidChecker.checkGuid(userGuid)) {
            this.shoppingListService.delete(shoppingListId);

            return new ResponseEntity<String>(HttpStatus.OK);

        } else {
            return new ResponseEntity<String>(HttpStatus.UNAUTHORIZED);
        }
    }
}
